home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: netcom.com!marnold
- From: marnold@netcom.com (Matt Arnold)
- Subject: [Q] Best way to "define" flag values?
- Message-ID: <marnoldDLIv9w.1s4@netcom.com>
- Organization: NETCOM On-line Communication Services (408 261-4700 guest)
- Distribution: na
- Date: Sun, 21 Jan 1996 08:22:44 GMT
- Sender: marnold@netcom.netcom.com
-
- Hi,
-
- I know many C++ programmers strive to use the C preprocessor as
- little as possible, employing C++'s many language features instead.
- For example, most functional macros are much better implemented as
- function templates in C++.
-
- Another area where C++ programmers can avoid using C macros is
- when defining the constant numeric values used in their code.
- Instead of "#define THE_ANSWER 42", enums or const data values can
- be used with better type safety, etc..
-
- However, I have never quite found a good substitute for declaring
- values for flags. Passing flags to a function, for instance, is
- a very common construct, and it's typically arranged like so...
-
- #define FLAG_A 0x0001
- #define FLAG_B 0x0010
- #define FLAG_C 0x0100
-
- void FuncTakingFlags(unsigned flags);
- .
- .
- .
- FuncTakingFlags(FLAG_A | FLAG_B); // example usage
-
-
- I suppose what I am after is a way to automatically define flags,
- in the same fashion that an enum can be used to "automatically"
- generate unqiue numeric values...
-
- enum { A, B, C };
-
- For example, A, B and C are all guaranteed to be different values
- and, I as a programmer don't even have to worry what they are if I
- don't need to. And, I can extend the value set by simply adding
- another member to the enum. Undoutbly, this is somewhat "cleaner"
- than #define-ing (or whatever) values manually.
-
- I've though about using enum behavior to automatically create flags,
- using something like the following...
-
- enum
- {
- flag_bit_a, // NOTE: relies on first enum member being zero
- flag_bit_b,
- flag_bit_c,
-
- FLAG_A = 1 << flag_bit_a,
- FLAG_B = 1 << flag_bit_b,
- FLAG_C = 1 << flag_bit_c
- };
-
- But, this seems to be just as "messy" and just as error-prone as
- the age-old #define approach and doesn't really seem to buy much.
- Plus, it intoduces "implementation" identifiers into the namespace
- (all the "bit" values used to create the flag values above).
-
- How do others deal with flags in their programming, especially
- when creating "API" or library functions for which passing flags
- is really the only choice (course, I am open to hearing about
- methods that even avoid flag values).
-
- Thanks for any advice or comments.
-
- Regards,
- -------------------------------------------------------------------------
- Matt Arnold | | ||| | |||| | | | || ||
- marnold@netcom.com | | ||| | |||| | | | || ||
- Boston, MA | 0 | ||| | |||| | | | || ||
- 617.389.7384 (h) 617.576.2760 (w) | | ||| | |||| | | | || ||
- C++, MIDI, Win32/95 developer | | ||| 4 3 1 0 8 3 || ||
- -------------------------------------------------------------------------
-